package org.abc.fund.service.funds;

import org.abc.fund.entity.funds.FundNetValueHistory;
import org.abc.fund.repository.funds.FundNetValueHistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class FundNetValueHistoryService {

    private final FundNetValueHistoryRepository repository;

    public FundNetValueHistoryService(FundNetValueHistoryRepository repository) {
        this.repository = repository;
    }

    public List<FundNetValueHistory> getHistoryByFundCode(String fundCode) {
        return repository.findByFundCodeOrderByNetDateDesc(fundCode);
    }

    public List<FundNetValueHistory> getHistoryInRange(String fundCode, LocalDate start, LocalDate end) {
        return repository.findByFundCodeAndNetDateBetweenOrderByNetDateDesc(fundCode, start, end);
    }

    public boolean existsByFundCodeAndNetDate(String fundCode, LocalDate netDate) {
        return !repository.findByFundCodeAndNetDate(fundCode, netDate).isEmpty();
    }

    public FundNetValueHistory addNetValue(FundNetValueHistory history) {
        return repository.save(history);
    }

    public void deleteById(Long id) {
        repository.deleteById(id);
    }
}
